package com.rainszj.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rainszj.pojo.po.Type;
import com.rainszj.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import static com.rainszj.constant.WebConstant.*;

/**
 * @author rainszj
 * 2020/3/12
 */
@Controller
public class TypeController {

    @Autowired
    TypeService typeService;

    /**
     * 后台分页展示所有的分类
     *
     * @param pageNum
     * @param model
     * @return
     */
    @GetMapping("/admin/types")
    public String list(@RequestParam(defaultValue = "1", name = "pageNum") Integer pageNum, Model model) {
        // 设置分页的大小，默认获取第一页，查询 7条内容
        PageInfo<Type> pageInfo = PageHelper.startPage(pageNum, PAGE_SIZE).doSelectPageInfo(() -> typeService.listAdminType());
        /*// 查询所有的分页
        List<Type> allTypes = typeService.listAdminType();
        // 得到分页结果对象
        PageInfo pageInfo = new PageInfo<Type>(allTypes);*/
        // 返回给前端
        model.addAttribute("pageInfo", pageInfo);
        return "admin/types";
    }


    /**
     * 根据 ID 删除分类
     *
     * @param id
     * @return
     */
    @DeleteMapping("/admin/types/{id}")
    public String deleteType(@PathVariable("id") Long id, RedirectAttributes attributes) {
        // 先查询后删除
        String typeName = typeService.queryTypeById(id).getName();
        int result = typeService.removeType(id);
        System.err.println("[typeName]=>" + typeName);
        if (result >= 0) {
            attributes.addFlashAttribute("msg", typeName + "删除成功");
        } else {
            attributes.addFlashAttribute("msg", typeName + "删除失败");
        }


        return "redirect:/admin/types";
    }

    /**
     * 跳转到修改分类页面
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/admin/types/{id}")
    public String toUpdateType(@PathVariable("id") Long id, Model model) {
        Type type = typeService.queryTypeById(id);
        model.addAttribute("type", type);

        return "admin/type-upd";
    }

    /**
     * put方式，修改分类
     *
     * @return
     */
    @PutMapping("/admin/types/{id}")
    public String updateType(@PathVariable("id") Long id, Type type, RedirectAttributes attributes) {


        int result = typeService.updateType(id, type);
        if (result != 0) {
            attributes.addFlashAttribute("msg", type.getName() + "修改成功");
        } else {
            attributes.addFlashAttribute("msg", type.getName() + "修改失败");
        }

        return "redirect:/admin/types";
    }

    /**
     * 跳转到添加分类页面
     *
     * @return
     */
    @GetMapping("/admin/addType")
    public String toAddType() {
        return "admin/type-add";
    }

    /**
     * 添加分类
     *
     * @param type
     * @return
     */
    @PostMapping("/admin/types")
    public String addType(Type type, RedirectAttributes attributes) {

        Type queryType = typeService.queryTypeByName(type.getName());
        if (queryType != null) {
            attributes.addFlashAttribute("message", "不能添加重复的分类");
            return "redirect:/admin/addType";
        }


        int result = typeService.saveType(type);
        if (result != 0) {
            attributes.addFlashAttribute("msg", type.getName() + "添加成功");
        } else {
            attributes.addFlashAttribute("msg", type.getName() + "添加失败");
        }

        return "redirect:/admin/types";
    }

}
